<?php

// +--------------------------------------------------------
// | Name: 星空后台 基础信息配置模型
// +---------------------------------------------------------
// | Version: V5.0
// +---------------------------------------------------------
// | Author: xkyinzhe <zhonghuaxinxing@sina.cn>
// +---------------------------------------------------------
// | Time: 2021-04-09 13:40
// +---------------------------------------------------------
declare(strict_types=1);

namespace app\common\model;

use think\model\concern\SoftDelete;

/**
 * @xk 基础信息配置
 */
class Config extends BaseModel
{
    use SoftDelete;
    protected $name = 'webConfig';
    protected $pk = 'config_id';
    protected $deleteTime = 'delete_time';

    /**
     * @xk 新增前操作
     *
     * @method onBeforeInsert
     *
     * @param [type] $data [description]
     *
     * @return [type] [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-04-09T13:58:40+080
     */
    public static function onBeforeInsert($data)
    {
    }

    /**
     * @xk 更新前操作
     *
     * @method onBeforeUpdate
     *
     * @param [type] $data [description]
     *
     * @return [type] [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-04-09T13:58:51+080
     */
    public static function onBeforeUpdate($data)
    {
    }

    /**
     * @xk 写入前操作
     *
     * @method onBeforeWrite
     *
     * @param [type] $data [description]
     *
     * @return [type] [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-04-09T13:59:02+080
     */
    public static function onBeforeWrite($data)
    {
        unset($data['act']);
        unset($data['controllers']);
        unset($data['actions']);
        if (!$data->create_time) {
            $data->update_time = $data->create_time = date('Y-m-d H:i:s');
        }
        $data->update_time = date('Y-m-d H:i:s'); // 操作时间 实际发布时间
    }
    /**
     * @xk 写入后操作 更新到配置文件中
     *
     * @method onAfterWrite
     * @param  [type]                  $data                     [description]
     * @return [type]                                            [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-04-19T10:32:06+080
     */
    public static function onAfterWrite($data){
      $list = self::select();
      $fileurl = '../config/web_config.php';
      $arr = [];
      if ($list) {
          foreach ($list as $key => $value) {
              if($value['type_id'] == 2){
                $arr[$value['sign']] = getShowUrl($value['content']);
              }else{
                $arr[$value['sign']] = $value['content'];
              }
          }
          $content = "<?php \r\n//+--------------------------------------------------------------------\r\n// | Name：星空框架 网站基础信息\r\n//+--------------------------------------------------------------------\r\n// | Version：V5.0\r\n//+--------------------------------------------------------------------\r\n// | Author: xkyinzhe <zhonghuaxinxing@sina.cn>\r\n//+--------------------------------------------------------------------\r\n// | City: 青岛\r\n//+--------------------------------------------------------------------\r\n// | Time: ".date('Y-m-d H:i:s')."\r\n//+--------------------------------------------------------------------\r\n return ".var_export($arr,true).';';
          return file_put_contents($fileurl, $content);
      }
    }
}
